package com.ysd.gulimall.product.web;

import com.ysd.gulimall.product.entity.CategoryEntity;
import com.ysd.gulimall.product.feign.CouponFeignService;
import com.ysd.gulimall.product.service.CategoryService;
import com.ysd.gulimall.product.vo.Catelog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    CouponFeignService couponFeignService;

    @GetMapping({"/","/index.html"})
    public String indexPage(Model model) {
      //拿到一级分类
        List<CategoryEntity> categoryEntities= categoryService.getLevel1Categorys();
        model.addAttribute("category",categoryEntities);
        try {
            Object r=couponFeignService.listFeign();
            model.addAttribute("banners",r);
        }catch (Exception e){
            System.out.println("远程超时、轮播图获取失败");
        }
        return "index";
    }
    @ResponseBody
    @GetMapping({"/app/banners"})
    public Map<String,Object> indexApp() {
        //拿到一级分类
        List<CategoryEntity> categoryEntities= categoryService.getLevel1Categorys();

        Object r=couponFeignService.listFeign();
        Map<String,Object> m=new HashMap();
        m.put("banners",r);
        return m;
    }
    @ResponseBody
    @GetMapping("index/catalog.json")
    public Map<String,List<Catelog2Vo>> getCatlogJson(){
        Map<String, List<Catelog2Vo>> catalogJson = categoryService.getCatalogJson();
        return catalogJson;
    }
}


